﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public partial class View_Admin_QuanLyTinTuc : System.Web.UI.Page
{
    QLTinTuc tt = new QLTinTuc();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            dropLoaiTT.DataSource = tt.Load_LoaiTinTuc();
            dropLoaiTT.DataValueField = "Maloai";
            dropLoaiTT.DataTextField = "Tenloai";
            dropLoaiTT.DataBind();

            GridView1.DataSource = tt.Load_TinTuc();
            GridView1.DataBind();

            lblNgayDang.Text = DateTime.Now.Date.ToString();
        }
    }
    void LuuNoiDungTT()
    {
        string filename = Server.MapPath("~/News/"+txtTieuDe.Text + ".txt");
        FileStream fs = new FileStream(filename, FileMode.Create);
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(fs, txtNoiDung.Text);
        fs.Close();
    }
    protected void btnDangTin_Click(object sender, EventArgs e)
    {
        LuuNoiDungTT();
        tt.Insert_TinTuc(txtTieuDe.Text, "~/News/" + txtTieuDe.Text + ".txt", DateTime.Parse(lblNgayDang.Text), dropLoaiTT.SelectedValue.ToString());
        GridView1.DataSource = tt.Load_TinTuc();
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //string ngaydang = (string)GridView1.Rows[e.NewEditIndex].Cells[2].Text.ToString();
        //ngaydang = DateTime.Now.Date.ToString();

        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = tt.Load_TinTuc();
        GridView1.DataBind();
        GridView1.Rows[e.NewEditIndex].Cells[0].Enabled = false;
        txtRead_NoiDung.Visible = true;

        DropDownList drop_LoaiHoa = (DropDownList)GridView1.Rows[e.NewEditIndex].Cells[4].FindControl("DropDownList1");
        drop_LoaiHoa.DataSource = tt.Load_LoaiTinTuc();
        drop_LoaiHoa.DataValueField = "MaLoai";
        drop_LoaiHoa.DataTextField = "TenLoai";
        drop_LoaiHoa.DataBind();
        drop_LoaiHoa.Items.FindByValue(GridView1.DataKeys[e.NewEditIndex].Values[0].ToString()).Selected = true;

        //doc file noi dung
        TextBox tentt = (TextBox)GridView1.Rows[e.NewEditIndex].Cells[1].Controls[0];
        string filename = Server.MapPath("~/News/" + tentt.Text + ".txt");
        if (!File.Exists(filename))
        {
            txtRead_NoiDung.Text = "Chưa cập nhật";
            return;
        }
        FileStream fs = new FileStream(filename, FileMode.Open);
        try
        {
            BinaryFormatter formatter = new BinaryFormatter();
            string noidung = (string)formatter.Deserialize(fs);
            txtRead_NoiDung.Text = noidung;
        }
        catch (Exception)
        {
            txtRead_NoiDung.Text = "tập tin đã bị chuyển nội dung!!!";
        }
        fs.Close();

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GridView1.DataSource = tt.Load_TinTuc();
        GridView1.DataBind();

        txtRead_NoiDung.Text = "";
        txtRead_NoiDung.Visible = false;
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox matt = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0];
        TextBox tentt = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
        TextBox ngaydang = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
        DropDownList loaitin = (DropDownList)GridView1.Rows[e.RowIndex].Cells[3].FindControl("DropDownList1");

        string filename = Server.MapPath("~/News/" + tentt.Text + ".txt");
        FileStream fs = new FileStream(filename, FileMode.Create);
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(fs, txtRead_NoiDung.Text);
        fs.Close();

        GridView1.DataSource = tt.Update_TinTuc(matt.Text, tentt.Text, "~/News/" + tentt.Text + ".txt", ngaydang.Text, loaitin.SelectedValue.ToString());
        GridView1.EditIndex = -1;
        GridView1.DataSource = tt.Load_TinTuc();
        GridView1.DataBind();

        txtRead_NoiDung.Text = "";
        txtRead_NoiDung.Visible = false;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string matt = (string)GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();
        GridView1.DataSource = tt.Delete_TinTuc(matt);
        GridView1.DataSource = tt.Load_TinTuc();
        GridView1.DataBind();
    }
}
